package com.css.dzj.zfjg.modules.cohostrelate.controller;


import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.cohostrelate.entity.JgLhjgRwxbglbEntity;
import com.css.dzj.zfjg.modules.cohostrelate.service.JgLhjgRwxbglbService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;


/**
 * 协办关系表
 *
 * @author zch
 * @date 2020-12-04 15:20:02
 */
@RestController
@RequestMapping("generator/jglhjgrwxbglb")
public class JgLhjgRwxbglbController {
    @Autowired
    private JgLhjgRwxbglbService jgLhjgRwxbglbService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgRwxbglbEntity> rwxbglQueryResults = jgLhjgRwxbglbService.queryPage(pageRequest);
            baseService.logSuccess(3, "分页查询所有任务协办关联信息成功", "2");
            return R.ok().put("data", rwxbglQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询所有任务协办关联信息失败", "2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rwxbId}")
    public R info(@RequestParam String rwxbId){
        try {
            JgLhjgRwxbglbEntity jgLhjgRwxbglb = jgLhjgRwxbglbService.findById(rwxbId);
            baseService.logSuccess(3, "通过id获取任务协办关联信息成功", "2");
            return R.ok().put("jgLhjgRwxbglb", jgLhjgRwxbglb);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取任务协办关联信息失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgRwxbglbEntity jgLhjgRwxbglb, HttpServletRequest request){
        try {
            if(StringUtils.isEmpty(jgLhjgRwxbglb)) {
                return R.error("协办信息不能空");
            }
            jgLhjgRwxbglbService.add(jgLhjgRwxbglb, request);
            baseService.logSuccess(3, "任务协办关联信息添加成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "任务协办关联信息添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgRwxbglbEntity jgLhjgRwxbglb){
        try {
            JgLhjgRwxbglbEntity oldJgLhjgRwxbglbEntity = jgLhjgRwxbglbService.findById(jgLhjgRwxbglb.getRwxbId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgRwxbglb, oldJgLhjgRwxbglbEntity);
            jgLhjgRwxbglbService.update(jgLhjgRwxbglb);
            baseService.logSuccess(3, "任务协办关联信息修改成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "任务协办关联信息修改失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam String rwxbId){
        try {
            JgLhjgRwxbglbEntity jgLhjgRwxbglbEntity = jgLhjgRwxbglbService.findById(rwxbId);
            jgLhjgRwxbglbService.delete(jgLhjgRwxbglbEntity);
            baseService.logSuccess(3, "任务协办关联信息删除成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "任务协办关联信息删除失败", "2");
            return R.error(e.getMessage());
        }
    }

}
